package com.team.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Data
@AllArgsConstructor
@Slf4j
@NoArgsConstructor
@Component
public class AliOssUtil
    {

        @Value("${team.aliyun.endpoint}")
        private String endpoint;
        @Value("${team.aliyun.access-key-id}")
        private String accessKeyId;
        @Value("${team.aliyun.access-key-secret}")
        private String accessKeySecret;
        @Value("${team.aliyun.bucket-name}")
        private String bucketName;

        /**
         * 文件上传
         *
         * @param file 将前端传的MultipartFile直接传入即可
         * @return
         */
        public String upload(MultipartFile file) throws IOException
            {
                //生成存储的文件名
                String originalFilename = file.getOriginalFilename();
                String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

                // 创建OSSClient实例。
                OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

                try
                    {
                        // 创建PutObject请求。
                        ossClient.putObject(bucketName, filename, file.getInputStream());
                    } catch (OSSException oe)
                    {
                        System.out.println("Caught an OSSException, which means your request made it to OSS, "
                                + "but was rejected with an error response for some reason.");
                        System.out.println("Error Message:" + oe.getErrorMessage());
                        System.out.println("Error Code:" + oe.getErrorCode());
                        System.out.println("Request ID:" + oe.getRequestId());
                        System.out.println("Host ID:" + oe.getHostId());
                    } catch (ClientException ce)
                    {
                        System.out.println("Caught an ClientException, which means the client encountered "
                                + "a serious internal problem while trying to communicate with OSS, "
                                + "such as not being able to access the network.");
                        System.out.println("Error Message:" + ce.getMessage());
                    } finally
                    {
                        if (ossClient != null)
                            {
                                ossClient.shutdown();
                            }
                    }

                //文件访问路径规则 https://BucketName.Endpoint/ObjectName
                StringBuilder stringBuilder = new StringBuilder("https://");
                stringBuilder
                        .append(bucketName)
                        .append(".")
                        .append(endpoint)
                        .append("/")
                        .append(filename);

                log.info("文件上传到:{}", stringBuilder.toString());

                return stringBuilder.toString();
            }
    }
